﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Design;
using System.Management;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Police_Question_ProblemList : System.Web.UI.Page
{
    private bool Flag = true;
    private int PageCount = 15;
    private string ConstDeparmentID;
    protected void Page_Load(object sender, EventArgs e)
    {
        ConstDeparmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (!IsPostBack)
        {
            ListPageContral1.SetFirstPage();
            Bind(PageCount, 0);
            Flag = true;
        }
    }

    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        Bind(pageSize, PageIndex);
    }

    private void Bind(int pageSize, int pageIndex)
    {

        QuestionDataContext db = new QuestionDataContext();

        if (Flag)
        {
            var result = (from s in db.SecurityProblem where  s.Sid == new Guid(Request["ID"]) select s);
            var obj = result.Skip(pageSize * pageIndex).Take(pageSize);
            this.UserGrid.DataSource = obj;
            ListPageContral1.ItemCount = result.Count();
            this.UserGrid.DataBind();
        }
        else
        {
            var obj = (from s in db.SecurityProblem where s.Title.Contains(txtTitle.Text) select s);
            var result = obj.Skip(pageSize * pageIndex).Take(pageSize);
            this.UserGrid.DataSource = result;
            ListPageContral1.ItemCount = obj.Count();
            this.UserGrid.DataBind();
        }


    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ListPageContral1.CurrentPageIndex) * ListPageContral1.PageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }

    public string SubString(object obj)
    {
        if (obj == null)
            return "";

        if (obj.ToString().Length > 30)
            return obj.ToString().Substring(0, 30) + "...";
        else
            return obj.ToString();
    }

    public string GetDepartmentName(object obj)
    {
        try
        {
            if (obj == null)
                return "";
            else
                return Yipusoft.Account.BLL.Department.GetDepartment(obj.ToString()).ShortName;
        }
        catch
        {
            return "";
        };


    }
    protected void lBtnEdit_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                if (Val(Request["ID"]))
                    return;

                Response.Redirect("ProblemEdit.aspx?ID=" + UserGrid.DataKeys[i].Value.ToString()+"&pid="+Request["ID"]);
                break;
            }
        }
        Prompt.Text = "警告：请选择记录";
    }
    protected void lBtnQuery_Click(object sender, EventArgs e)
    {
        if (txtTitle.Text.Trim() == "")
        {
            Flag = true;
            Response.Redirect("QuestionList.aspx");
        }
        else
        {
            Flag = false;
        }
        ListPageContral1.SetFirstPage();
        Bind(PageCount, 0);


    }
    protected void lBtnDelete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                if (Val(Request["ID"]))
                    return;

                QuestionDataContext db = new QuestionDataContext();
                var obj = from s in db.SecurityProblem where s.Id == new Guid(UserGrid.DataKeys[i].Value.ToString()) select s;
                db.SecurityProblem.DeleteAllOnSubmit(obj);
                db.SubmitChanges();

            }
        }
        ListPageContral1.SetFirstPage();
        Bind(PageCount, 0);
    }
    protected void lBtnAddCaption_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProblemList.aspx");
    }
    protected void lBtnAddProblem_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                if (Val(UserGrid.DataKeys[i].Value.ToString()))
                    return;
                Response.Redirect("ProblemNew.aspx?ID=" + UserGrid.DataKeys[i].Value.ToString());
            }
        }
        Response.Redirect("ProblemNew.aspx");
    }
    private bool Val(string ID)
    {
        QuestionDataContext db1 = new QuestionDataContext();
        var obj = (from o in db1.SecuritySubject where o.Id == new Guid(ID) select o).FirstOrDefault();

        if (obj.CreateDepartment != ConstDeparmentID)
        {
            lblPrompt.Text = "该试卷是由总队下达你不可以修改";
            return true;

        }
        return false;
    }
}
